# from curl_cffi import requests

url = 'https://weibo.com/ajax/feed/hottimeline?refresh=2&group_id=102803&containerid=102803&extparam=discover%7Cnew_feed&max_id=2&count=10'
import requests
import os
import shutil

if os.path.exists('static/微博头像'):
    shutil.rmtree('static/微博头像')
os.mkdir('static/微博头像')

cookies = {
    'UOR': 'www.sogou.com,weibo.com,www.sogou.com',
    'SINAGLOBAL': '5591677354833.484.1753952532185',
    'ULV': '1753952532188:1:1:1:5591677354833.484.1753952532185:',
    'SUB': '_2AkMeWdEsf8NxqwFRmvwcy2rrb4p_ywrEieKoBSD3JRMxHRl-yT9kqlIptRB6Ndn_wxX41jiSi6HvLrRGj0W7XRoxfWVU',
    'SUBP': '0033WrSXqPxfM72-Ws9jqgMF55529P9D9WhgTG_jsDkIGBrYXDp5wcVE',
    'XSRF-TOKEN': 'PKkjSXUHufrd5vgWUZMWElFc',
    'WBPSESS': 'cbjWrs_UvapT2Pg_hlK_fLnlPmV7WYRbEBvXvtGCeD0qfB8gUnYhMGI8_sx8jk_-Dv4742iMDANAMlHkOBBFg_4bpNGCuffgWzCvJgknNBsfWO47Lg0ae_joinAYcaa8',
}

headers = {
    'referer': 'https://weibo.com/newlogin?tabtype=weibo&gid=102803&openLoginLayer=0&url=https%3A%2F%2Fwww.weibo.com%2F',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36',
}

params = {
    'refresh': '2',
    'group_id': '102803',
    'containerid': '102803',
    'extparam': 'discover|new_feed',
    'max_id': '1',
    'count': '10',
}

res = requests.get(url, params=params, headers=headers,cookies= cookies)
datas = res.json()
# print(datas['statuses'])
for data in datas['statuses']:
    screen_name = data['user']['screen_name']
    # profile_image_url = data['user']['profile_image_url']
    avatar_large = data['user']['avatar_large']
    image_url = requests.get(avatar_large)
    with open(f'static/微博头像/{screen_name}.jpg', 'wb') as f:
        f.write(image_url.content)
    # # print(image_url.status_code)
    # print({'screen_name': screen_name, 'profile_image_url': profile_image_url})
